package bancosys.tec.rtm.impl.diagnostician;

import java.io.InputStream;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * Diagnosticador que verifica se um xml está bem estruturado de acordo com seu schema.
 * 
 * @author Evaristo
 * @version $Id:$ <b>Created on: </b> 27/02/2007
 */
public class XmlSchemaValidatorDiagnostician extends AbstractDiagnostician {

    private String xmlName;

    private InputStream xmlInputStream;

    private InputStream schemaInputStream;

    /**
     * Construtor do Diagnosticador de Validador de Xml por Schema.
     * 
     * @param xmlName String
     * @param xmlInputStream InputStream
     * @param schemaInputStream InputStream
     */
    public XmlSchemaValidatorDiagnostician(String xmlName, InputStream xmlInputStream, InputStream schemaInputStream) {
        this.xmlName = xmlName;
        this.xmlInputStream = xmlInputStream;
        this.schemaInputStream = schemaInputStream;
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return "Validador de XML por Schema (" + xmlName + ")";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema = factory.newSchema(new StreamSource(this.schemaInputStream));
        schema.newValidator().validate(new StreamSource(this.xmlInputStream));
        return true;
    }
}